package com.cszy.yydqbfq.ui.activity

import android.Manifest
import android.app.Activity
import android.content.Context
import android.net.Uri
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import com.cszy.yydqbfq.databinding.ActivityCreatSongListAcTivityBinding
import com.cszy.yydqbfq.base.BaseVMActivity

import com.cszy.yydqbfq.dialog.PermissoTipDialog
import com.cszy.yydqbfq.dialog.UserRefusedPermissonDialog
import com.cszy.yydqbfq.ext.toast
import com.cszy.yydqbfq.utils.SpUtils
import com.cszy.yydqbfq.viewmodel.SongListViewModel
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions


class CreatSongListActivity : BaseVMActivity<SongListViewModel, ActivityCreatSongListAcTivityBinding>() {

    private var pernissonTopdialog: PermissoTipDialog? = null
    private var coverUri:String?=null
    override fun initView(savedInstanceState: Bundle?) {
     mBinding.headTitle.headCenterTitle.text="创建歌单"
    }


    override fun initListener() {
       mBinding.headTitle.headBack.setOnClickListener {
           finish()
       }

        mBinding.ivAddCover.setOnClickListener {
            if (XXPermissions.isGranted(
                    this,
                    android.Manifest.permission.READ_MEDIA_IMAGES
                )
            ) {
                selectImage()

            } else {
                request_Permission(
                    this,
                    Manifest.permission.READ_MEDIA_IMAGES,
                    "相册权限",
                    "需要获取您的手机相册权限"
                )
            }

        }

        mBinding.rvCreatSongger.setOnClickListener {
           if (mBinding.edtName.text.toString().isEmpty()){
               toast("请输入歌单名称")
               return@setOnClickListener
           }
            mViewModel.addPlaylist(mBinding.edtName.text.toString(),coverUri?:"")
            toast("创建成功")
            finish()
        }
    }
    // 调用图片选择器
    fun selectImage() {
        getContent.launch("image/*")
    }
    private fun request_Permission(
        context: Activity,
        req_permisson: String,
        permisson_name: String,
        content: String
    ) {
        // 检查是否已标记为"不再询问"
        if (!SpUtils.getInstance().getBoolean(permisson_name)) { // 没有获取该权限或者没有永久拒绝
            showPermissonTipsDialog(context, content)
        }
        XXPermissions.with(context)
            // 申请单个权限
            .permission(req_permisson)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    if (pernissonTopdialog != null) {
                        pernissonTopdialog!!.dismiss()
                    }
                    if (!allGranted) {

                        return
                    }
                    SpUtils.getInstance().putBoolean(permisson_name, true)
                    toast("获取 $permisson_name 权限成功")
                    selectImage()
                }

                override fun onDenied(
                    permissions: MutableList<String>,
                    doNotAskAgain: Boolean
                ) {
                    if (doNotAskAgain) {
                        if (pernissonTopdialog != null) {
                            pernissonTopdialog!!.dismiss()
                        }
                        showDoNotAskAgainDialog(
                            context,
                            req_permisson,
                            permisson_name
                        )
                    } else {
                        if (pernissonTopdialog != null) {
                            pernissonTopdialog!!.dismiss()
                        }

                    }
                }
            })
    }
    /**
     * 用户单次拒绝
     */
    fun showPermissonTipsDialog(context: Context, content: String) {
        pernissonTopdialog = PermissoTipDialog(context)
        pernissonTopdialog!!.setPermissionMessage(content)
        pernissonTopdialog!!.show()
        pernissonTopdialog!!.setCancelable(false)
    }

    /**
     * 用户多次拒绝
     */
    fun showDoNotAskAgainDialog(context: Context, permission: String, permissionName: String) {
        var dialog = UserRefusedPermissonDialog(context,permission)
        dialog.setPermissionMessage("您未授予 $permissionName 权限，请去设置中开启")
        dialog.setCancelable(false)
        dialog.show()
    }

    // 在 Activity/Fragment 中
    val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
        uri?.let {
            // 处理选中的图片 URI
             mBinding.ivAddCover.setImageURI(it)
             coverUri=it.toString()
        }
    }


}